<template>
    <div class="common-page page-staff-corp-company-orders" v-loading.fullscreen="loading">
        <div class="common-page__head">
            <comp-paths class="route" :paths="['/staff/corp', '/staff/corp/companys', '/staff/corp/company?companyId=' + companyId]"></comp-paths>
            <div class="title" v-if="company">
                <div class="text">{{ company.name }}</div>
                <el-button type="plain" size="small" icon="el-icon-plus" @click="addOrder(company)" v-if="self">提交事项</el-button>
            </div>
            <div class="title" v-else></div>
        </div>
        <comp-tabs class="common-page__head" :tabs="['/staff/corp/company?companyId=' + companyId, $route.fullPath, '/staff/corp/company/products?companyId=' + companyId, '/staff/corp/company/demands?companyId=' + companyId, '/staff/corp/company/attendances?companyId=' + companyId]"></comp-tabs>
        <comp-company-orders class="common-page__body" ref="list" :companyId="companyId" select link>
            <!-- <template #head="scope">
                <el-button type="plain" size="small" icon="el-icon-refresh" @click="search()">刷新列表</el-button>
            </template> -->
            <template #body="scope">
                <el-button type="primary" size="mini" @click.stop="getOrder(scope.data)" v-if="access('staff-work')">详情</el-button>
            </template>
        </comp-company-orders>
    </div>
</template>

<script>
    import mixin from "../mixin";
    import { string, int, cache } from "../util";
    export default {
        name: "page-staff-corp-company-orders",
        title: "事项清单",
        nodes: ["/menu/staff-corp"],
        childs: [],
        mixins: [mixin],
        data: () => ({
            loading: false,
            companyId: "",
            company: null,
            self: false,
        }),
        methods: {
            search() {
                if (this.loading) return;
                this.$nextTick(() => this.$refs.list.init());
            },
            addOrder(company) {
                if (this.loading) return;
                this.$router.push("/staff/work/order/add?companyId=" + company.id);
            },
            getOrder(order) {
                if (this.loading) return;
                this.$router.push("/staff/work/order?orderId=" + order.id);
            },
            destroy() {
                if (this.loading) return;
                return true;
            },
            init() {
                if (this.loading) return;
                let query = this.$route.query;
                let company = null;
                this.loading = true;
                this.companyId = query.companyId;
                this.company = cache(query.companyId);
                this.$root.$refs.navbar.init();
                this.plugins.user.call(async () => {
                    let res = await this.plugins.user.init();
                    if (res.code === 0) { }
                    return res;
                }, async () => {
                    if (this.access(this.$route.name)) return { code: 0 };
                    return this.env.xhrErrors.find((error) => error.code === 4);
                }, async () => {
                    let res = await this.plugins.user.getCompany(query.companyId);
                    if (res.code === 0) company = res.data;
                    return res;
                }).then((res) => {
                    this.loading = false;
                    this.$root.$refs.navbar.init();
                    if (res.code === 0) {
                        this.company = cache(query.companyId, company);
                        this.self = this.company.employees.some((employee) => employee.client.id === this.profile.id);
                        this.search();
                    } else if (res.code === 2) {
                        this.$root.$refs.navbar.login();
                    } else {
                        this.plugins.modal.alert("页面加载失败", res.code + "：" + res.msg, "error").then(() => this.$router.replace("/staff/corp/companys"));
                    }
                });
            },
        },
    };
</script>

<style lang="scss">
    .page-staff-corp-company-orders {
        overflow: hidden;
    }
</style>